package TopSky.IOT.DAO;

import Hia.Utils.JdbcUtils;
import TopSky.IOT.Models.controldevice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class ControlDeviceDAO {
	
	public Map<Integer, controldevice> getMap(int i,int k){
		Map<Integer, controldevice> map = new HashMap<Integer, controldevice>();
		String sql = "select * from controldevice limit ?,?";
		Connection con  = null;
		PreparedStatement pre = null;
		ResultSet rs = null;
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			pre.setInt(1, i);
			pre.setInt(2, k);
			rs = pre.executeQuery();
			for(int n = 0;rs.next();n++){
				controldevice controldevice = maping(rs);
				map.put(n, controldevice);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return map;
	}
	public Map<Integer, controldevice> getMap(){
		Map<Integer, controldevice> map = new HashMap<Integer, controldevice>();
		String sql = "select * from controldevice order by Intime desc ";
		Connection con  = null;
		PreparedStatement pre = null;
		ResultSet rs = null;
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			for(int n = 0;rs.next();n++){
				controldevice controldevice = maping(rs);
				map.put(n, controldevice);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return map;
	}
	
	private controldevice maping(ResultSet rs) throws SQLException{
		controldevice controldevice = new controldevice();
		controldevice.setID(rs.getInt("Id"));
		controldevice.setLabsID(rs.getInt("UserID"));
		controldevice.setName(rs.getString("Name"));
		controldevice.setIntime(rs.getTimestamp("Intime"));
		controldevice.setPicture(rs.getString("Picture"));
		controldevice.setInfo(rs.getString("LabInfo"));
		return controldevice;
	}
		
	public controldevice getById(int i){
		controldevice controldevice = new controldevice();
		Connection con  = null;
		PreparedStatement pre = null;
		ResultSet rs = null;
		String sql = "select * from controldevice where ID = ?";
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			pre.setInt(1, i);
			rs = pre.executeQuery();
			for(int n = 0;rs.next();n++){
				controldevice = maping(rs);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return controldevice;
	}


	public int InsertOne(controldevice controldevice){
		String sql = "insert into controldevice"
				+ "(LabsID,Name,Intime,Picture,Info) values(?,?,?,?,?)";
		Connection con = null;
		PreparedStatement pre = null;
		int i = 0;
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			pre.setInt(1, controldevice.getLabsID());	
			pre.setString(2, controldevice.getName());
			pre.setTimestamp(3, controldevice.getIntime());
			pre.setString(4, controldevice.getPicture());
			pre.setString(5, controldevice.getInfo());
			i = pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}

	public int UpById(int i,controldevice controldevice){
		String sql = "update controldevice set LabID = ? and InTime = ? and Email = ?"
				+ " and LabInfo = ? and Name = ? and Passwd = ? and Picture = ? and Profession = ? where Id = ?";
		Connection con = null;
		PreparedStatement pre = null;
		int n = 0;
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			pre.setInt(1, controldevice.getID());
			pre.setTimestamp(2, controldevice.getIntime());
			pre.setString(4, controldevice.getInfo());
			pre.setString(5, controldevice.getName());
			pre.setString(7, controldevice.getPicture());
			pre.setInt(9, i);
			n = pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	public int DeleteById(int i){
		String sql = "delete from controldevice where LabID = ?";
		Connection con = null;
		PreparedStatement pre = null;
		int n = 0;
		try {
			con = JdbcUtils.getConnection();
			pre = con.prepareStatement(sql);
			pre.setInt(1, i);			
			n = pre.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}	
}
